package com.mirai.mirai_boot.service.impl;

import catcode.CatCodeUtil;
import com.mirai.mirai_boot.api.BaiduBaikeApi;
import com.mirai.mirai_boot.entity.MyMsg;
import com.mirai.mirai_boot.service.BaidubaikeService;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.sender.Sender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author : CP
 * @version: 1.0
 * @program : simbot_mirai_springboot
 * @description :
 * @date : 2021-06-29 07:25
 **/
@Service
public class BaidubaikeServiceImpl implements BaidubaikeService {

    @Autowired
    private BaiduBaikeApi baiduBaikeApi;

    @Override
    public void findBaike(String para, GroupMsg msg, Sender sender) throws IOException {
        if (para!=null){
            // 百科查询方法
            MyMsg findBaike = baiduBaikeApi.getFindBaike(para);

            if(findBaike == null){
                // 无内容
                sender.sendGroupMsg(msg,"无相关百科");
            }else if (findBaike.getImgUrl()==null||findBaike.getImgUrl().equals("")){
                // 没图片
                sender.sendGroupMsg(msg,findBaike.getContent());
            }else {
                // 获取猫猫码工具
                CatCodeUtil util = CatCodeUtil.INSTANCE;
                String image = util.toCat("image",  "url="+findBaike.getImgUrl());
                sender.sendGroupMsg(msg,findBaike.getContent()+image);
            }
        }else {
            sender.sendGroupMsg(msg,"百科 您要百科的内容");
        }
    }
}
